package com.example.hans.navigationbyviewpageandradiobutton;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener , View.OnClickListener{

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private TextView textView1, textView2, textView3, textView4;
    private List<View> views;
    private List<TextView> textViewList;
    private MyPageAdapter myPageAdapter;
    LocalActivityManager localActivityManager;

    private LinearLayout firstLinearLayout;
    private LinearLayout secondLinearLayout;
    private LinearLayout threeLinearLayout;
    private LinearLayout fourLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        localActivityManager = new LocalActivityManager(this,true);

        localActivityManager.dispatchCreate(savedInstanceState);

        findView();


    }

    public void findView() {

        textViewList = new ArrayList<>();

        viewPager = findViewById(R.id.viewpager);
        viewPager.setOnPageChangeListener(this);

        firstLinearLayout = findViewById(R.id.firstLinearLayout);
        firstLinearLayout.setOnClickListener(this);
        secondLinearLayout = findViewById(R.id.secondLinearLayout);
        secondLinearLayout.setOnClickListener(this);
        threeLinearLayout = findViewById(R.id.threeLinearLayout);
        threeLinearLayout.setOnClickListener(this);
        fourLinearLayout = findViewById(R.id.fourLinearLayout);
        fourLinearLayout.setOnClickListener(this);


        //使用adioButton部分
//        radioGroup = findViewById(R.id.radiogroup);
//        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//
//                for (int i = 0; i < radioGroup.getChildCount(); i++) {
//                    if (radioGroup.getChildAt(i).getId() == checkedId) {
//                        viewPager.setCurrentItem(i);
//                        whiltLineVisibility(i);
//                        break;
//                    }
//                }
//            }
//        });
//
//        textView1 = findViewById(R.id.txtv_buttom1);
//        textView2 = findViewById(R.id.txtv_buttom2);
//        textView3 = findViewById(R.id.txtv_buttom3);
//        textView4 = findViewById(R.id.txtv_buttom4);
//        textViewList.add(textView1);
//        textViewList.add(textView2);
//        textViewList.add(textView3);
//        textViewList.add(textView4);
        setPage();


    }

    public void whiltLineVisibility(int position) {
        for (int i = 0; i < textViewList.size(); i++) {
            if (i == position) {
                textViewList.get(i).setVisibility(View.VISIBLE);
            } else {
                textViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setPage() {
        views = new ArrayList<>();


        //用activity填充视图
        Intent in1 = new Intent(getApplicationContext(),FirstActivity.class);
        views.add(getView("first",in1));
        Intent in2 = new Intent(getApplicationContext(),SecondActivity.class);
        views.add(getView("second",in2));
        Intent in3 = new Intent(getApplicationContext(),ThirdActivity.class);
        views.add(getView("third",in3));
        Intent in4 = new Intent(getApplicationContext(),FourActivity.class);
        views.add(getView("four",in4));


        //用布局填充视图
//        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
//
//        View view1 = inflater.inflate(R.layout.first,null);
//        View view2 = inflater.inflate(R.layout.second,null);
//        View view3 = inflater.inflate(R.layout.third,null);
//        View view4 = inflater.inflate(R.layout.four,null);
//
//        views.add(view1);
//        views.add(view2);
//        views.add(view3);
//        views.add(view4);



        myPageAdapter = new MyPageAdapter(views);
        viewPager.setAdapter(myPageAdapter);
        viewPager.setCurrentItem(0);

        updateBottomLinearLayoutSelect(true,false,false,false);

    }

    public View getView(String id,Intent intent) {
        return localActivityManager.startActivity(id,intent).getDecorView();
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
//        whiltLineVisibility(i);

        boolean flag[] = new boolean[4];
        for (int j = 0; j < 4; j++) {
            if (j == i) {
                flag[j] = true;
            } else {
                flag[j] = false;
            }
        }
        updateBottomLinearLayoutSelect(flag[0],flag[1],flag[2],flag[3]);

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.firstLinearLayout: {
                viewPager.setCurrentItem(0);
                updateBottomLinearLayoutSelect(true,false,false,false);
                break;
            }
            case R.id.secondLinearLayout: {
                viewPager.setCurrentItem(1);
                updateBottomLinearLayoutSelect(false,true,false,false);
                break;
            }
            case R.id.threeLinearLayout: {
                viewPager.setCurrentItem(2);
                updateBottomLinearLayoutSelect(false,false,true,false);
                break;
            }
            case R.id.fourLinearLayout: {
                viewPager.setCurrentItem(3);
                updateBottomLinearLayoutSelect(false,false,false,true);
                break;
            }
        }
    }

    public void updateBottomLinearLayoutSelect(boolean first,boolean second,boolean three, boolean four) {
        firstLinearLayout.setSelected(first);
        secondLinearLayout.setSelected(second);
        threeLinearLayout.setSelected(three);
        fourLinearLayout.setSelected(four);
    }
}
